package com.kld.yqyj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.yqyj.domain.bo.TInjwwGroupProMonBo;
import com.kld.yqyj.domain.vo.TInjwwGroupProMonVo;
import com.kld.yqyj.domain.TInjwwGroupProMon;
import com.kld.yqyj.mapper.TInjwwGroupProMonMapper;
import com.kld.yqyj.service.ITInjwwGroupProMonService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 井组月度生产数据Service业务层处理
 *
 * @author bw
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class TInjwwGroupProMonServiceImpl implements ITInjwwGroupProMonService {

    private final TInjwwGroupProMonMapper baseMapper;

    /**
     * 查询井组月度生产数据
     *
     * @param injmcId 井组月度生产数据主键
     * @return 井组月度生产数据
     */
    @Override
    public TInjwwGroupProMonVo queryById(String injmcId){
        return baseMapper.selectVoById(injmcId);
    }

    /**
     * 查询井组月度生产数据列表
     *
     * @param bo 井组月度生产数据
     * @return 井组月度生产数据
     */
    @Override
    public TableDataInfo<TInjwwGroupProMonVo> queryPageList(TInjwwGroupProMonBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TInjwwGroupProMon> lqw = buildQueryWrapper(bo);
        Page<TInjwwGroupProMonVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询井组月度生产数据列表
     *
     * @param bo 井组月度生产数据
     * @return 井组月度生产数据
     */
    @Override
    public List<TInjwwGroupProMonVo> queryList(TInjwwGroupProMonBo bo) {
        LambdaQueryWrapper<TInjwwGroupProMon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TInjwwGroupProMon> buildQueryWrapper(TInjwwGroupProMonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TInjwwGroupProMon> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOTotalNum() != null, TInjwwGroupProMon::getOTotalNum, bo.getOTotalNum());
        lqw.eq(bo.getOJkNum() != null, TInjwwGroupProMon::getOJkNum, bo.getOJkNum());
        lqw.eq(bo.getDLiSp() != null, TInjwwGroupProMon::getDLiSp, bo.getDLiSp());
        lqw.eq(bo.getDOSp() != null, TInjwwGroupProMon::getDOSp, bo.getDOSp());
        lqw.eq(bo.getDWSp() != null, TInjwwGroupProMon::getDWSp, bo.getDWSp());
        lqw.eq(bo.getInjDSp() != null, TInjwwGroupProMon::getInjDSp, bo.getInjDSp());
        lqw.eq(bo.getWCut() != null, TInjwwGroupProMon::getWCut, bo.getWCut());
        lqw.eq(bo.getPjPfl() != null, TInjwwGroupProMon::getPjPfl, bo.getPjPfl());
        return lqw;
    }

    /**
     * 新增井组月度生产数据
     *
     * @param bo 井组月度生产数据
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TInjwwGroupProMonBo bo) {
        TInjwwGroupProMon add = BeanUtil.toBean(bo, TInjwwGroupProMon.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setInjmcId(add.getInjmcId());
        }
        return flag;
    }

    /**
     * 修改井组月度生产数据
     *
     * @param bo 井组月度生产数据
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TInjwwGroupProMonBo bo) {
        TInjwwGroupProMon update = BeanUtil.toBean(bo, TInjwwGroupProMon.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TInjwwGroupProMon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除井组月度生产数据
     *
     * @param injmcIds 需要删除的井组月度生产数据主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
